//
//  WebLoadingViewController.swift
//  Chigo
//
//  Created by 谢成蹊 on 2020/4/30.
//  Copyright © 2020 东信北邮信息技术有限公司. All rights reserved.
//

import UIKit
import WebKit
import SnapKit
import JavaScriptCore
class WebLoadingViewController: UIViewController ,WKNavigationDelegate, WKScriptMessageHandler{
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        print("-------------" + message.name)

//        if message.name == "goBackByIOS" {
//            self.navigationController?.popViewController(animated: true)
//            }else if message.name == "getUserTokenByJS" {
//
//                var token = ""
//            
//                if((appDelegate?.currentUser) != nil){
//                    token =  (appDelegate?.currentUser!.userToken)!
//
//                }
//
//                webView.evaluateJavaScript("getUserInfoByIOS('\(token)')", completionHandler: nil)
//
//            }
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        //web加载好了才有左边按钮
        print("加载完成")
        
        if(tag == 0){
            tag = 1
            let myURL = URL(string: filePath)
            let myRequest = URLRequest(url: myURL!)

            webView.load(myRequest)
            
        }
        
        
    }
    

    var name: String = ""
    
    var filePath:String = ""
    var context = JSContext()
    
    var tag = 0
    
    lazy var webView: WKWebView = {
            let preferences = WKPreferences()
    //preferences.javaScriptEnabled = true
            let configuration = WKWebViewConfiguration()
            configuration.preferences = preferences
            configuration.userContentController = WKUserContentController()
            //注册changePwdByJS这个函数,让js调用
            configuration.userContentController.add(self, name: "goBackByIOS")
            configuration.userContentController.add(self, name: "getUserTokenByJS")
            var webView = WKWebView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height), configuration: configuration)
        

            webView.scrollView.bounces = true
            webView.scrollView.alwaysBounceVertical = true
            webView.navigationDelegate = self
            return webView
        }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "视频通话"

        self.view.backgroundColor = UIColor.white

        view.addSubview(webView)
        //加载H5页面
        let myURL = URL(string: filePath)
        let myRequest = URLRequest(url: myURL!)

        webView.load(myRequest)
        
        
        let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        btn1.setImage(UIImage(named: "back"), for: .normal)
        btn1.addTarget(self, action: #selector(backClick), for: .touchUpInside)
        let item1 = UIBarButtonItem(customView: btn1)
        self.navigationItem.leftBarButtonItem = item1

    }
    
    @objc func backClick(){
        self.navigationController?.popViewController(animated: true)
    }
    
//    override func viewWillAppear(_ animated: Bool) {
//        self.navigationController?.navigationBar.isHidden = true
//    }
//
//
//    override func viewWillDisappear(_ animated: Bool) {
//       self.navigationController?.navigationBar.isHidden = false
//    }

}
